<?php
class matchController extends base
{

    public function __construct($type='api')
    {
        parent::__construct($type);
    }
    /**
     * 赛事主页
     */
    public function domatchInfo()
    {
        $match_id = common::request('match_id','G');
        if(empty($match_id)){
            $_ENV['error']->getErrors(101);
            exit;
        }
        
        $uid = $this->uid;
        $match_mod = $this->load('match');
        $matchInfo = $match_mod->getMatchInfo($match_id);
        $addInfo = [
            "user_add_id" => null,
            "uid" => $uid,
            "name" => null,
            "phone" => null,
            "gender" => null,
            "status" => null,
            "group_add_info" => []
        ];
        $userLastInfo = [
            'name' => '',
            'phone' => '',
            'gender' => ''
        ];
        
        $groupCanJoin = 1;
        if($uid){
            $addInfo = $match_mod->getMatchAdd($match_id,$uid);
            $userLastInfo = $match_mod->getUserLastInfo($uid);
            //群组是否可以报名
            if($addInfo && $addInfo['group_add_info']){
                $created = $this->load('group')->myGroupListByRole($uid, 1);
                $manager = $this->load('group')->myGroupListByRole($uid, 2);
                $g1 = [];
                foreach($created as $row){
                    $g1[] = $row['gid'];
                }
                foreach($manager as $row){
                    $g1[] = $row['gid'];
                }
                $g2 = [];
                foreach($addInfo['group_add_info'] as $row){
                    $g2[] = $row['gid'];
                }
                if(array_intersect($g1,$g2)){
                    $groupCanJoin = 0;
                }
            }
        }
        
        //商品
        $goods_mod = $this->load('goods');
        $_goodsList = $goods_mod->getRaceGoodsList($matchInfo['match_id']);
        $goodsList = [];
        $sort = [];
        foreach($_goodsList as $goods){
            $goods['attrs'] = $goods_mod->getAllAttrByPid($goods['product']['id']);
            $sort[] = $goods['sort'];
            $goodsList[] = $goods;
        }
        array_multisort($sort,$goodsList);
        
        //订单
        if($uid){
            $order = $this->load('order')->getOrderByCond(['order_id'],"match_id=$match_id AND uid=$uid");
            $order_id = $order ? $order['order_id'] : '';
        }else{
            $order_id = null;
        }
        $this->result['result'] = compact('matchInfo','addInfo','userLastInfo','groupCanJoin','goodsList','order_id');
    }
    
    /*
     * 报名
     */
    public function dojoinMatch(){
        $match_id = common::request('match_id','P');
        if(empty($match_id)){
            $_ENV['error']->getErrors(101);
            exit;
        }
        
        $uid = $this->uid;
    	if(!$uid)
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        
        $type = common::request('type','P','user');
        $gid = common::request('gid','P');
        if($type=='group' && empty($gid)){
            $_ENV['error']->getErrors(101);
            exit;
        }
        $goods_id = common::request('goods_id','P','');
        $name = common::request('name','P');
        $phone = common::request('phone','P');
        $gender = common::request('gender','P');
        if($type=='user' && (empty($name) || empty($phone) || empty($gender))){
            $_ENV['error']->getErrors(101);
            exit;
        }
        $match_mod = $this->load('match');
        $matchInfo = $match_mod->getMatchInfo($match_id);
        if(empty($matchInfo)){
            $_ENV['error']->getErrors(101);
            exit;
        }
        if($matchInfo['limit_member_max'] && $matchInfo['limit_member_max'] <= $matchInfo['user_count']){
            $_ENV['error']->getErrors(2011);
            exit;
        }
        
        if($type=='group'){
            $status = 1;
        }elseif($type=='user'){
            if(!$goods_id){
                $status = 1;
                $this->in_erpDb();
                $this->erpDb->query("update r_order set status=3 where pay_status='10' and match_id='$match_id' and uid='$uid'");
            }elseif($goods_id){
                $status = 0;
            }
        }
        $match_add_id = $match_mod->joinMatch($match_id,$type,$uid,$name,$phone,$gender,$gid,$status);
        
        if(!$match_add_id){
            $_ENV['error']->getErrors(2012);
            exit;
        }
        
        $this->result['result'] = [
            'match_add_id'=>$match_add_id,
            'is_success'=>$match_add_id ? 1:0
        ];
    }
    
    /*
     * 证书
     */
    public function docertificate(){
        $match_id = common::request('match_id','G');
        if(empty($match_id)){
            $_ENV['error']->getErrors(101);
            exit;
        }
        
        $uid = $this->uid;
    	if(!$uid)
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $match_mod = $this->load('match');
        $matchInfo = $match_mod->getMatchInfo($match_id);
        $certificate = $match_mod->getCertificate($matchInfo,$uid);
        if($certificate['uid']){
            $user = $this->load('user')->getUserInfo($certificate['uid']);
            $certificate['nickname'] = $user['nickname'];
        }
        /*
        if($certificate['gid']){
            $group = $this->load('group')->getGroupInfo($certificate['gid']);
            $certificate['group_name'] = $group['name'];
        }
        */
        $matchInfo = array_merge($matchInfo,$certificate);
        
        $this->result['result'] = $matchInfo;
    }
    
    /*
     * 数据排行
     */
    public function dorank(){
        $match_id = common::request('match_id','G');
        if(empty($match_id)){
            $_ENV['error']->getErrors(101);
            exit;
        }
        $uid = $this->uid;
        $match_mod = $this->load('match');
        $matchInfo = $match_mod->getMatchInfo($match_id);
        $now = time();
        $from = $now >= $matchInfo['etime'] ? 'dabiao' : 'all';
        $rank = $match_mod->getRank($match_id,$matchInfo['match_type'],$from);
        $user_score = $uid ? $match_mod->getUserScore($match_id,$uid) : (object)[];
        
        $this->result['result'] = compact('matchInfo','rank','user_score');
    }
}